package com.scoy.honeymei.activity.login;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.Nullable;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.scoy.honeymei.MeApp;
import com.scoy.honeymei.R;
import com.scoy.honeymei.activity.LmggActivity;
import com.scoy.honeymei.activity.MainActivity;
import com.scoy.honeymei.base.BaseActivity;
import com.scoy.honeymei.url.MeConstant;
import com.scoy.honeymei.url.MyUrl;
import com.scoy.honeymei.utils.MyUtil;
import com.scoy.honeymei.utils.SPHelper;

import org.json.JSONException;
import org.json.JSONObject;

import io.dcloud.adnative.UniAdManager;
import io.dcloud.adnative.model.IShowAwardVideoListener;
import io.dcloud.adnative.model.ISplashADListener;
import io.dcloud.adnative.util.DoToast;
import io.dcloud.adnative.util.Logger;

/**
 * @ClassName: WelcomeActivity$
 * @Description: 启动页
 * @Author: GaoZhan
 * @CreateDate: 2019/8/30/0030$ 上午 10:29$
 * @UpdateUser: 更新者
 * @UpdateDate: 2019/8/30/0030$ 上午 10:29$
 * @UpdateRemark: 更新内容
 */
public class WelcomeActivity extends BaseActivity {

    private WelcomeActivity mContext;
    private CountDownTimer count;
    private int isADs = 0;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);

        mContext = WelcomeActivity.this;

        if (SPHelper.getInt("sure", 0) == 1) {
            MeApp.initLate();
        }

        initNormal();

        httpImg();

    }

    @Override
    public void initNormal() {
        MyUtil.setStatusBar(mContext, getWindow(), true, 0);
    }


    private void mestart() {

        count = new CountDownTimer(1000 * 2, 500) {
            @Override
            public void onTick(long millisUntilFinished) {

            }

            @Override
            public void onFinish() {
                if(type == 2){
                    gglm();
                }else{
                    Intent intent = new Intent(mContext, /*AdsActivity*/Ads2Activity.class);
                    // intent.putExtra("adimg", SPHelper.getString("startImg", ""));
                    startActivity(intent);
                    finish();
                }



              /*  if (isADs == 1) {
                    Intent intent = new Intent(mContext, AdsActivity.class);
                    intent.putExtra("adimg", SPHelper.getString("startImg", ""));
                    startActivity(intent);

                } else {
                    if (!"null".equals(SPHelper.getString(MeConstant.PHONE, "null")) && !"null".equals(SPHelper.getString(MeConstant.UID, "null"))) {
                        startActivity(new Intent(mContext, MainActivity.class));
                    } else {
                        startActivity(new Intent(mContext, LoginActivity.class));
                    }
                }*/
            }
        }.start();

    }

    private void gglm(){

        // 演示设置了底价的激励视频广告位使用。 1733738477 是设置了底价的高价值广告位，1510833300是普通广告位
        UniAdManager.AwardVideoOption awardVideoOption = new UniAdManager.AwardVideoOption();
        // 注意 这里
        awardVideoOption.awardPosId = "1884727747";

        awardVideoOption.adHeight = 1920;
        awardVideoOption.adWidth = 1080;

        UniAdManager.showAwardVideo(WelcomeActivity.this,
                awardVideoOption,
                new IShowAwardVideoListener() {

                    @Override
                    public void onError(int code, String message) {
                        Log.i("HHHHHHHHH："+code,message);
                        if (!"null".equals(SPHelper.getString(MeConstant.PHONE, "null")) && !"null".equals(SPHelper.getString(MeConstant.UID, "null"))) {
                            startActivity(new Intent(mContext, MainActivity.class));
                        } else {
                            startActivity(new Intent(mContext, LoginActivity.class));
                        }
                        finish();
                    }

                    @Override
                    public void onAllFail() {
                        Log.i("HHHHHHHHH：","1");
                        if (!"null".equals(SPHelper.getString(MeConstant.PHONE, "null")) && !"null".equals(SPHelper.getString(MeConstant.UID, "null"))) {
                            startActivity(new Intent(mContext, MainActivity.class));
                        } else {
                            startActivity(new Intent(mContext, LoginActivity.class));
                        }
                        finish();
                    }

                    @Override
                    public void onHandleClose() {
                        Logger.d("onHandleClose  ");
                        Log.i("HHHHHHHHH：","2");
                        if (!"null".equals(SPHelper.getString(MeConstant.PHONE, "null")) && !"null".equals(SPHelper.getString(MeConstant.UID, "null"))) {
                            startActivity(new Intent(mContext, MainActivity.class));
                        } else {
                            startActivity(new Intent(mContext, LoginActivity.class));
                        }
                        finish();
                    }

                    @Override
                    public void onVideoComplete() {
                        Logger.d("onVideoComplete  ");
                        Log.i("HHHHHHHHH：","3");
                        if (!"null".equals(SPHelper.getString(MeConstant.PHONE, "null")) && !"null".equals(SPHelper.getString(MeConstant.UID, "null"))) {
                            startActivity(new Intent(mContext, MainActivity.class));
                        } else {
                            startActivity(new Intent(mContext, LoginActivity.class));
                        }
                        finish();
                    }

                    @Override
                    public void onADClick() {
                        Logger.d("onADClick  ");
                        Log.i("HHHHHHHHH：","4");
                    }

                    @Override
                    public void onRewardGet() {
                        Logger.d("onRewardGet  ");
                        Log.i("HHHHHHHHH：","5");
                    }

                    @Override
                    public void onADShow() {
                        Logger.d("onADShow  ");
                        Log.i("HHHHHHHHH：","6");
                    }

                    @Override
                    public void onSkipVideo() {
                        Logger.d("onSkipVideo  ");
                        Log.i("HHHHHHHHH：","7");
                    }

                });
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (count != null) {
            count.cancel();
        }
    }

//    private void httpImg() {
//        OkGo.<String>post(MyUrl.COMMENT_IMG).execute(new StringCallback() {
//            @Override
//            public void onSuccess(Response<String> response) {
//                String body = response.body();
//                BaseBean baseBean = new Gson().fromJson(body, BaseBean.class);
//                String code = baseBean.getCode();
//                String data = (String) baseBean.getData();
//                if ("200".equals(code)) {
//
//                    if (data != null && data.length() > 0) {
//                        isADs = 1;
//                        SPHelper.applyString("startImg", data);
//                    } else {
//                        if (!"".equals(SPHelper.getString("startImg", ""))) {
//                            isADs = 1;
//                        } else {
//                            isADs = 0;
//                        }
//                    }
//
//                } else {
//                    isADs = 0;
//                }
//            }
//        });
//
//    }

    private int type = 0; // 1--视频；2--广告联盟
    private String app;
    private void httpImg() {
        OkGo.<String>post(MyUrl.APP_AD_IMG).execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                String body = response.body();
                try {
                    JSONObject jsBean = new JSONObject(body);
                    String code = jsBean.getString("code");
                    String msg = jsBean.getString("msg");
                    Object data = jsBean.get("data");
                    String sdata = data.toString();
                    if ("200".equals(code)) {
                        JSONObject jsb = new JSONObject(sdata);
                        type = jsb.getInt("gg_type");
                        app = jsb.getString("app");
                    }
                    Log.i("AAAA：","SSSSSSS");
                    mestart();
                } catch (JSONException e) {
                    e.printStackTrace();
                    mestart();
                }

            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
            }
        });


    }
}
